package com.yehebl.orm.data.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.yehebl.orm.exception.ConnectionException;


/**
 * 获取数据库连接,用于临时连接生成实体类
 * @author Administrator
 *
 */

public class ConnectionManager {
	
	private final String url;
	private final String className;
	private final String user;
	private final String password;
	
	public ConnectionManager(String url,String user,String password){
		this.url=url;
		this.user=user;
		this.password=password;
		this.className="com.mysql.jdbc.Driver";
		try {
			Class.forName(this.className);
		} catch (ClassNotFoundException e) {
			throw new Error(e);
		} 
	}
	public ConnectionManager(String url,String className,String user,String password) {
		this.url=url;
		this.className=className;
		this.user=user;
		this.password=password;
		try {
			Class.forName(this.className);
		} catch (ClassNotFoundException e) {
			throw new Error(e);
		} 
	}
	
	
	public Connection getConnection() {
        try {
			return DriverManager.getConnection(this.url, this.user, this.password);
		} catch (SQLException e) {
			throw new ConnectionException(e);
		}  
	}
}
